/*
 * Powered By 
 * Since 2010 - 2016
 */

package com.rf.business.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.business.bean.ShuttleBean;
import com.rf.business.dao.ShuttleMapper;
import com.rf.business.model.Shuttle;
import com.rf.business.model.ShuttleExample;
import com.rf.business.service.ShuttleService;
import com.rf.sys.dao.SqlAdapterMapper;
import com.rf.sys.model.Department;
import com.rf.sys.model.User;
import com.rf.sys.service.DepartmentService;
import com.rf.sys.service.UserService;
import com.rf.sys.utils.PageUtils;

@Service("shuttleService")
public class ShuttleServiceImpl implements ShuttleService {
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
   	@Autowired
   	private ShuttleMapper shuttleMapper;//注入dao

	@Resource
	private UserService userService;
   	/**
	 * 根据id获取数据
	 * @param shuttleId
	 * @return Shuttle
	 */
   	@Override
	public Shuttle getShuttleByID(String id
) throws Exception{
		// TODO Auto-generated method stub
		return shuttleMapper.selectByPrimaryKey(id
);
	}
	
	/**
	 * 根据Shuttle获取数据
	 * @param Shuttle
	 * @return List<Shuttle>
	 */
	@Override
	public List<Shuttle> getShuttle(Shuttle shuttle) throws Exception{
		// TODO Auto-generated method stub
		ShuttleExample example = new ShuttleExample();
		com.rf.business.model.ShuttleExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		List<Shuttle> list = shuttleMapper.selectByExample(example);
		return list;
	}
	
	/**
	 * 获取所有数据
	 * @param 
	 * @return List<Shuttle>
	 */
	@Override
	public List<Shuttle> getShuttle() throws Exception{
		// TODO Auto-generated method stub
		ShuttleExample example = new ShuttleExample();
		com.rf.business.model.ShuttleExample.Criteria criteria= example.createCriteria();
		
		//criteria.andStateNotEqualTo(ConfigUtils.DELSTATE);//不查询已删除的
		return shuttleMapper.selectByExample(example);
	}
   	
	/**
	 * 保存信息
	 * @param Shuttle
	 * @return
	 */
	@Override
	public void addShuttle(Shuttle shuttle) throws Exception{
		// TODO Auto-generated method stub
		shuttleMapper.insert(shuttle);
	}

	/**
	 * 修改数据
	 * @param Shuttle
	 * @param
	 */
	@Override
	public void updateShuttle(Shuttle shuttle) throws Exception{
		// TODO Auto-generated method stub
		shuttleMapper.updateByPrimaryKeySelective(shuttle);
	}
	
	/**
	 * 删除数据
	 * @param shuttleId
	 * @param
	 */
	@Override
	public void deleteShuttle(String id
) throws Exception{
		// TODO Auto-generated method stub
		shuttleMapper.deleteByPrimaryKey(id
);
	}

	/**
	 * 获取数据分页
	 * @param Shuttle
	 * @param pageUtils
	 * @return Map
	 */
	@Override
	public Map getShuttlePage(Shuttle shuttle,PageUtils pageUtils) throws Exception{
		ShuttleExample example = new ShuttleExample();
		com.rf.business.model.ShuttleExample.Criteria criteria= example.createCriteria();
		if(shuttle.getCompany() != null && !shuttle.getCompany().equals("") ){
			criteria.andCompanyLike("%"+shuttle.getCompany()+"%");
		}
		if(shuttle.getCarNo() != null && !shuttle.getCarNo().equals("")){
			criteria.andCarNoLike("%"+shuttle.getCarNo()+"%");
		}

		//获取第offset页，每页共limit条内容，默认查询总数count
		Page p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getRows());
		//下一条执行语句，即为要被转换的语句
		List<Shuttle> list = shuttleMapper.selectByExample(example);
		List<ShuttleBean> listBean = new ArrayList<ShuttleBean>();
		for(Shuttle s : list){
			ShuttleBean sB = new ShuttleBean();
			User u = userService.getUserByID(s.getCreateUser());
			sB.setId(s.getId());
			sB.setCarNo(s.getCarNo());
			sB.setCompany(s.getCompany());
			sB.setRemark(s.getRemark());
			sB.setCreateUser(s.getCreateUser());
			sB.setCreateTime(s.getCreateTime());
			sB.setState(s.getState());
			sB.setUserName(u.getUserName());
			listBean.add(sB);
		}
		Map dateMap = new HashMap();
	    dateMap.put("total", p.getTotal());
	    dateMap.put("rows", listBean);
		return dateMap;
	}
}

